﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using WP.Commons;
using WP.DB;
using WP.Modules;
using WP.Controls;
using WP.Controls.GridColumn;

using WP.RM.BusinessRule;
using WP.RM.BusinessLogical;
using WP.RM.DataAccess;

namespace WP.RM.Form
{
    public partial class frmRMSecondCategoryMtn :  BaseMtn  
    {
        private string mstrParentMaterialID = "";
        private string mstrParentMaterialName = "";
        public frmRMSecondCategoryMtn()
        {            
            InitializeComponent();
            LoadAllControl(new ArrayList(this.Controls));
            this.MtnTabControl = this.tbcMain;
            this.DesignTabsName = "tbpGeneral";
            tbcMain.DrawMode = TabDrawMode.OwnerDrawFixed;
            this.MinimumSize = this.Size;
        }

        public frmRMSecondCategoryMtn(string strParentMaterialID, string strParentMaterialName)
        {
            InitializeComponent();
            LoadAllControl(new ArrayList(this.Controls));
            this.MtnTabControl = this.tbcMain;
            this.DesignTabsName = "tbpGeneral";
            tbcMain.DrawMode = TabDrawMode.OwnerDrawFixed;
            this.MinimumSize = this.Size;

            mstrParentMaterialID = strParentMaterialID;
            mstrParentMaterialName = strParentMaterialName;
            initControl();
        }

        private void frmRMSecondCategoryMtn_Load(object sender, EventArgs e)
        {
            this.TableName = "rmSecondMaterialType";
           
            //dgList.ColumnWidth = "0,70,180,100,40,20,120,90,130";
            dgList.ConnectionString = DBAccess.ConnectionDBString;
            dgList.TableName = this.TableName;
            dgList.ColumnWidth = DataFunctions.GetGridColumnWidthString(this.Name, this.CurrentMenuInfo.iParameter, SysVar.UserName, this.Name + "." + this.dgList.Name);
            string strSql = WP.Commons.BaseFunctions.GetLookUpSQL(this.TableName, this.CurrentMenuInfo.iParameter);
            dgList.Sql = strSql;
            this.formStatus = FormStatus.Query;

            FillLookUpInfo();
            FillTree();
        }

        private void FillTree()
        {
            DBAccess dbcon = new DBAccess(false, false);
            string strSQL = "select TypeId,TypeName from rmMaterialFirstType order by TypeId";
            DataTable dtItem = dbcon.GetDataTable(strSQL);
            foreach (DataRow drType in dtItem.Rows)
            {
                TreeNode tvNode = new TreeNode(CvtFunc.ToString(drType["TypeId"]) + "-" + CvtFunc.ToString(drType["TypeName"]));
                this.tvTree.Nodes.Add(tvNode);
                int intRow=0;
                strSQL = "select MaterialTypeID,MaterialType from rmMaterialType where MaterialTypeID like '" + CvtFunc.ToString ( drType["TypeID"]) + "%'";
                DataTable dtDtl = dbcon.GetDataTable(strSQL);
                foreach (DataRow drDtl in dtDtl.Rows)
                {   
                    tvNode.Nodes.Add(CvtFunc.ToString(drDtl["MaterialTypeID"]) + "-" + CvtFunc.ToString(drDtl["MaterialType"]));
                }
            }
            dbcon.CloseCon();
        }


        public override void InitDeftValue()
        {
            base.InitDeftValue();
            if (this.formStatus == FormStatus.AddNew)
            {
                if (this.tvTree.SelectedNode == null) return;
                if (this.tvTree.SelectedNode.Nodes.Count == 0)
                {
                    txtCateID.Text = CvtFunc.ToString(this.tvTree.SelectedNode.Text.ToString()).Split('-')[0];
                    txtCateName.Text = CvtFunc.ToString(this.tvTree.SelectedNode.Text.ToString()).Split('-')[1];
                    txtCateFItemID.Text = DataFunctions.GetFieldValue("rmMaterialType", "CateFItemID", "CateID='" + txtCateID.Text + "'");
                }
            }
        }
        private void initControl()
        {
            this.Text = "物料价格类别维护";
            txtCategoryName.Text = mstrParentMaterialName;
            FillDtl(txtCategoryName.Text);

        }

        private bool mPressExit = false;
        private bool mblnSettingStatus = false;
        private bool[] mDtlColumnReadOnlyArray;
        private bool m_bCanTrigerCellValueChangedEven = false;
        private bool m_bSettingStatus = false;
        private bool m_bDtlChanged = false;
        private System.Windows.Forms.DataGridView.HitTestInfo mhtiDtl = null;
        private DBAccess mdbcon = new DBAccess(false, false);
        private string mstrTableName = "rmSecondMaterialType";
        private enum DtlCol
        {
            RowSeqNo = 0,
            ParentMaterialID,
            ParentMaterialName,
            CateID,
            CateName,
            MaterialID,
            MaterialName,
            StandardCost,
            PlanStandardCost,
            StandardCostUnit,
            RecID,
            ValueChanged,
            DelButton
        }

        protected override bool ProcessCmdKey(ref System.Windows.Forms.Message msg, System.Windows.Forms.Keys keyData) //激活回车键
        {
            int WM_KEYDOWN = 256;
            int WM_SYSKEYDOWN = 260;
            if (!(msg.Msg == WM_KEYDOWN | msg.Msg == WM_SYSKEYDOWN))
            {
                return false;
            }

            bool blnRet = false;
            if (BaseFunctions.TrigerKeyEventByControl(this.ActiveControl, this.dgvDetail.Name))
            {
                blnRet = dgvDetail_KeyDownEvent(keyData);
            }
            if (!blnRet)
            {
                return base.ProcessCmdKey(ref msg, keyData);
            }
            return blnRet;
        }

#region Comm For Mtn

        private void btnNew_Click(object sender, EventArgs e)
        {
            if (this.ConfirmChange())
            {
                if (!this.UpdateData()) return;
            }
            this.formStatus = FormStatus.AddNew;
        }
        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (!this.DeleteData()) return;
        }
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.formStatus = FormStatus.Delete;
        }
        private void btnExit_Click(object sender, EventArgs e)
        {
            if (this.formStatus == FormStatus.AddNew || this.formStatus == FormStatus.Update)
            {
                if (this.ConfirmChange())
                {
                    if (MessageBox.Show(SysVar.SaveChangeTitle, SysVar.WarnTitle, MessageBoxButtons.YesNo) == DialogResult.Yes)
                    {
                        if (!this.UpdateData()) return;
                    }
                }
            }
            this.ExitingStatus = true;
            mPressExit = true;
            this.Close();
        }
        private void btnHelp_Click(object sender, EventArgs e)
        {
            base.OnHelp();
        }
    
        private void btnNext_Click(object sender, EventArgs e)
        {
            Nevigate(this.dgList, NavigateOrient.Next);
        }
        private void btnPrev_Click(object sender, EventArgs e)
        {
            Nevigate(this.dgList, NavigateOrient.Prev);
        }
        private void btnTop_Click(object sender, EventArgs e)
        {
            Nevigate(this.dgList, NavigateOrient.Top);
        }
        private void btnBottom_Click(object sender, EventArgs e)
        {
            Nevigate(this.dgList, NavigateOrient.Bottom);
        }
        public override void SetNavigatorBarTooltip()
        {
            if (this.RecID <= 0)
            {
                this.btnBottom.ToolTipText = "定位到末页";
                this.btnTop.ToolTipText = "定位到首页";
                this.btnNext.ToolTipText = "定位到下一页";
                this.btnPrev.ToolTipText = "定位到前一页";
            }
            else
            {
                this.btnBottom.ToolTipText = "定位到末记录";
                this.btnTop.ToolTipText = "定位到首记录";
                this.btnNext.ToolTipText = "定位到下一条记录";
                this.btnPrev.ToolTipText = "定位到上一条记录";
            }
        }

        private void dgList_DefineColumn(object sender, EventArgs e)
        {
            base.SelectListViewField(dgList);
        }
        private void dgList_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter && dgList.CurrentRow != null)
            {
                this.RecID = Convert.ToInt64(dgList.RecID);
            }
        }
        private void dgList_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (dgList.CurrentRow != null && BaseFunctions.IsDataGridCell(this.dgList, e))
            {
                this.RecID = Convert.ToInt64(dgList.RecID);
            }
        }

        protected override void Field_Leave(object sender, System.EventArgs e)
        {
            base.Field_Leave(sender, e);
            Control con = sender as Control;
            if (con == null) return;

            //below is some speci field , need do some speci work
            if (con.Name == txtMaterialTypeID.Name)
            {
            }
        }
        protected override void Field_Change(object sender, EventArgs e)
        {
            base.Field_Change(sender, e);
            if (this.formStatus == FormStatus.Delete) return;
            Control con = sender as Control;
            if (con == null) return;

        }
        protected override void Field_KeyPress(object sender, KeyPressEventArgs e)
        {
            base.Field_KeyPress(sender, e);
        }

        private void tbcMain_Selecting(object sender, TabControlCancelEventArgs e)
        {
            if ((this.formStatus == FormStatus.Query || this.formStatus == FormStatus.Delete) &&
                this.dgList.Rows.Count == 0 && e.TabPageIndex > 0)
            {
                e.Cancel = true;
                return;
            }

            if (e.TabPageIndex == 0)
            {
                if (this.formStatus == FormStatus.AddNew || this.formStatus == FormStatus.Update)
                {
                    if (this.ConfirmChange())
                    {
                        if (MessageBox.Show(SysVar.SaveChangeTitle, SysVar.WarnTitle, MessageBoxButtons.YesNo) == DialogResult.Yes)
                        {
                            if (!this.UpdateData())
                            {
                                if (m_bTabPageNeedCancel)
                                    e.Cancel = true;

                                m_bTabPageNeedCancel = true;
                                return;
                            }
                        }
                    }
                    this.formStatus = FormStatus.Delete;
                }
                else if (this.formStatus == FormStatus.Query)
                {
                    this.formStatus = FormStatus.Delete;
                }
                this.dgList.RefreshData();
                this.dgList.Focus();
            }
            else if (e.TabPage == tabPage1)
            {
                this.UpdateData();
                FillDtl(); 
            }
            else if (e.TabPageIndex != 0 && this.formStatus != FormStatus.AddNew)
            {
                if (this.RecID == -1)
                    this.RecID = Convert.ToInt64(dgList.RecID);
                else
                {
                    string strErrMsg = "";
                    if (CheckDtlRow(ref strErrMsg))
                    {
                        if (this.dgvDetail.CurrentRow != null)
                        {
                            if (!this.Update_Detail(this.dgvDetail.CurrentRow))
                            {
                                e.Cancel = true;
                            }
                            else
                            {
                                DBAccess dbcon = new DBAccess(false, false);
                                string strSQL = "select * from rmSecondMaterialType where RecID='" + this.RecID + "'";
                                DataRow drRow = dbcon.GetDataFirstRow(strSQL);
                                if (drRow != null)
                                {
                                    SetControlValue(drRow["StandardCost"], txtStandardCost, true);
                                    SetControlValue(drRow["StandardCostUnit"], txtStandardCostUnit, true);
                                    SetControlValue(drRow["MaterialTypeName"], txtMaterialTypeName, true);
                                    this.TimeStamp = (byte[])drRow["RecStamp"];
                                }
                                dbcon.CloseCon();
                            }
                        }
                    }
                    else
                    {
                        e.Cancel = true;
                    }
                }
            }
            FillDtl();
        }
        private void tbcMain_DrawItem(object sender, DrawItemEventArgs e)
        {
            BaseFunctions.TabControl_DrawItem(sender, e, true);
        }
        private void FileMenu_Click(object sender, EventArgs e)
        {
            ToolStripMenuItem mnuItem = sender as ToolStripMenuItem;
            if (mnuItem != null)
            {
                switch (mnuItem.Name)
                {
                    case "newToolStripMenuItem":
                        btnNew_Click(sender, e);
                        break;
                    case "cancelToolStripMenuItem":
                        btnCancel_Click(sender, e);
                        break;
                    case "deleteToolStripMenuItem":
                        btnDelete_Click(sender, e);
                        break;
                    case "saveToolStripMenuItem":
                        btnUpdate_Click(sender, e);
                        break;
                    case "exitToolStripMenuItem":
                        btnExit_Click(sender, e);
                        break;
                }
            }
        }

        #endregion



        #region GRID
        private void InitGridDtl()
        {

            this.dgvDetail.Columns.Clear();
            this.dgvDetail.Rows.Clear();

            DataGridViewTextBoxColumn colRowSeqNo = new DataGridViewTextBoxColumn();
            colRowSeqNo.HeaderText = "序号";
            colRowSeqNo.Visible = true;
            colRowSeqNo.Name = "RowSeqNo";
            colRowSeqNo.Width = 50;
            colRowSeqNo.ReadOnly = true;

            DataGridViewTextBoxColumn colParentMaterialID = new DataGridViewTextBoxColumn();
            colParentMaterialID.HeaderText = "上级类别代码";
            colParentMaterialID.Visible = true;
            colParentMaterialID.Name = "ParentMaterialID";
            colParentMaterialID.Width = 50;
            colParentMaterialID.ReadOnly = true;
            colParentMaterialID.Visible = false;

            DataGridViewTextBoxColumn colParentMaterialName = new DataGridViewTextBoxColumn();
            colParentMaterialName.HeaderText = "上级类别名称";
            colParentMaterialName.Visible = true;
            colParentMaterialName.Name = "";
            colParentMaterialName.Width = 50;
            colParentMaterialName.ReadOnly = true;
            colParentMaterialName.Visible = false;

            TextLookUpColumn colCateID = new TextLookUpColumn();
            colCateID.HeaderText = "物料类别代码";
            colCateID.Visible = true;
            colCateID.Name = "CateID";
            colCateID.Width = 80;
            colCateID.ReadOnly = false ;

            DataGridViewTextBoxColumn colCateName = new DataGridViewTextBoxColumn();
            colCateName.HeaderText = "物料类别名称";
            colCateName.Visible = true;
            colCateName.Name = "CateName";
            colCateName.Width = 120;
            colCateName.ReadOnly = true;

            DataGridViewTextBoxColumn colMaterialID = new DataGridViewTextBoxColumn();
            colMaterialID.HeaderText = "价格类别代码";
            colMaterialID.Visible = true;
            colMaterialID.Name = "MaterialTypeID";
            colMaterialID.Width = 150;
            colMaterialID.ReadOnly = false ;

            DataGridViewTextBoxColumn colMaterialName = new DataGridViewTextBoxColumn();            
            colMaterialName.HeaderText = "价格类别名称";
            colMaterialName.Visible = true;
            colMaterialName.Name = "MaterialTypeName";
            colMaterialName.Width = 150;
            colMaterialName.ReadOnly = true ;

            DataGridViewTextBoxColumn colStandardCost = new DataGridViewTextBoxColumn();
            colStandardCost.HeaderText = "常用单价";
            colStandardCost.Width = 130;
            colStandardCost.Name = "StandardCost";
            colStandardCost.ReadOnly = false;
            colStandardCost.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight ;
            colStandardCost.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;


            DataGridViewTextBoxColumn colPlanStandardCost = new DataGridViewTextBoxColumn();
            colPlanStandardCost.HeaderText = "计划常用单价";
            colPlanStandardCost.Width = 130;
            colPlanStandardCost.Name = "PlanStandardCost";
            colPlanStandardCost.ReadOnly = false;
            colPlanStandardCost.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight;
            colPlanStandardCost.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;

            DataGridViewTextBoxColumn colStandardCostUnit = new DataGridViewTextBoxColumn();
            colStandardCostUnit.HeaderText = "单位";
            colStandardCostUnit.Visible = true;
            colStandardCostUnit.Name = "StandardCostUnit";
            colStandardCostUnit.Width = 80;
            colStandardCostUnit.ReadOnly = false ;

            DataGridViewTextBoxColumn colRecID = new DataGridViewTextBoxColumn();
            colRecID.HeaderText = "RecID";
            colRecID.Visible = false;
            colRecID.Name = "RecID";

            DataGridViewTextBoxColumn colValueChanged = new DataGridViewTextBoxColumn();
            colValueChanged.HeaderText = "ValueChanged";
            colValueChanged.Visible = false;
            colValueChanged.Name = "ValueChanged";

            DataGridViewImageColumn colDel = new DataGridViewImageColumn();
            Bitmap delbitmap = new Bitmap(this.iglSource.Images["del.ico"]);
            colDel.Width = 20;
            colDel.Name = "DelLine";
            colDel.HeaderText = "";
            colDel.Resizable = DataGridViewTriState.False;
            colDel.Visible = false;

            this.dgvDetail.Columns.AddRange(new DataGridViewColumn[] { colRowSeqNo, colParentMaterialID, colParentMaterialName,colCateID,colCateName , colMaterialID, colMaterialName, colStandardCost, colPlanStandardCost, colStandardCostUnit, colRecID, colValueChanged, colDel });

            mDtlColumnReadOnlyArray = new bool[dgvDetail.Columns.Count];
            string[] ColumnWidthArray = DataFunctions.GetGridColumnWidthString(this.Name, this.CurrentMenuInfo.iParameter, SysVar.UserName, this.Name + "." + this.dgvDetail.Name).Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            for (int i = 0; i < dgvDetail.Columns.Count; i++)
            {
                mDtlColumnReadOnlyArray[i] = dgvDetail.Columns[i].ReadOnly;
                if (ColumnWidthArray.Length == dgvDetail.Columns.Count) dgvDetail.Columns[i].Width = CvtFunc.ToInt(ColumnWidthArray[i]);
            }

            this.dgvDetail.EditMode = DataGridViewEditMode.EditOnEnter;
            this.dgvDetail.RowTemplate.Height = 20;
            this.dgvDetail.RowHeadersWidth = 23;
            this.dgvDetail.RowHeadersBorderStyle = DataGridViewHeaderBorderStyle.None;
            this.dgvDetail.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing;
            this.dgvDetail.ColumnHeadersHeight = 30;
            this.dgvDetail.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
            this.dgvDetail.SelectionMode = DataGridViewSelectionMode.CellSelect;
            this.dgvDetail.AllowUserToResizeRows = false;

        }

        private void FillDtl()
        {
            txtCategoryName.Text = "";
            FillDtl("");
        }

        private void FillDtl(string strCategoryName)
        {
            InitGridDtl();
            string strSql = "";
            string strUserName = "";
            string strRecid = "";
            bool bAccess = false;
            m_bCanTrigerCellValueChangedEven = false;

            this.dgvDetail.Rows.Clear();
            DBAccess dbCon = new DBAccess(false, false);
            if ( strCategoryName =="")
                strSql = "select B.* from rmSecondMaterialType B order by B.MaterialTypeID";
            else
                strSql = "select B.* from rmSecondMaterialType B where B.MaterialTypeName='" + txtCategoryName.Text + "' order by B.MaterialTypeID";

            DataTable mUserAlertTable = dbCon.GetDataSet(strSql).Tables[0];

            for (int i = 0; i < mUserAlertTable.Rows.Count; i++)
            {
                DataRow dr = mUserAlertTable.Rows[i];
                if (this.dgvDetail.Rows.Count <= i) this.dgvDetail.Rows.Add();
                dgvDetail.Rows[dgvDetail.Rows.Count - 1].Cells[(int)DtlCol.RowSeqNo].Value = dgvDetail.Rows.Count.ToString();
                dgvDetail.Rows[dgvDetail.Rows.Count - 1].Cells[(int)DtlCol.ParentMaterialID].Value = "";// dr["ParentMaterialID"].ToString().TrimEnd();
                dgvDetail.Rows[dgvDetail.Rows.Count - 1].Cells[(int)DtlCol.ParentMaterialName].Value = "";// dr["MaterialType"].ToString().TrimEnd();
                dgvDetail.Rows[dgvDetail.Rows.Count - 1].Cells[(int)DtlCol.CateID].Value = CvtFunc.ToString(dr["CateID"]);
                dgvDetail.Rows[dgvDetail.Rows.Count - 1].Cells[(int)DtlCol.CateName].Value = CvtFunc.ToString(dr["CateName"]); 
                dgvDetail.Rows[dgvDetail.Rows.Count - 1].Cells[(int)DtlCol.MaterialID].Value = dr["MaterialTypeID"].ToString().TrimEnd();
                dgvDetail.Rows[dgvDetail.Rows.Count - 1].Cells[(int)DtlCol.MaterialName].Value = CvtFunc.ToString(dr["MaterialTypeName"].ToString());
                dgvDetail.Rows[dgvDetail.Rows.Count - 1].Cells[(int)DtlCol.StandardCost].Value = BaseFunctions.FormatNumeric(dr["StandardCost"].ToString(), "Ns.4");
                dgvDetail.Rows[dgvDetail.Rows.Count - 1].Cells[(int)DtlCol.PlanStandardCost].Value = BaseFunctions.FormatNumeric(dr["PlanStandardCost"].ToString(), "Ns.4");
                dgvDetail.Rows[dgvDetail.Rows.Count - 1].Cells[(int)DtlCol.StandardCostUnit].Value = CvtFunc.ToString(dr["StandardCostUnit"].ToString());

                dgvDetail.Rows[dgvDetail.Rows.Count - 1].Cells[(int)DtlCol.RecID].Value = CvtFunc.ToInt(dr["RecID"]);
                dgvDetail.Rows[dgvDetail.Rows.Count - 1].Cells[(int)DtlCol.ValueChanged].Value = "0";
            }//
            if (dgvDetail.Rows.Count == 0)
            {
                AddDtlRow(this.dgvDetail.Name);
            }
            dbCon.CloseCon();
            base.SetBlankImage(dgvDetail);
            m_bCanTrigerCellValueChangedEven = true;
        }
        private void dgvDetail_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

            if (dgvDetail.Columns[e.ColumnIndex] is TextLookUpColumn)
            {
                if ((dgvDetail.Columns[e.ColumnIndex] as TextLookUpColumn).CanLookUp())
                {
                    Dtl_LookUp(e.ColumnIndex);
                }
            }
            else
            {
                if (this.dgvDetail.CurrentCell.ColumnIndex == (int)DtlCol.DelButton && base.HasDelImage(this.dgvDetail, e))
                {
                    this.dgvDetail.CurrentCell.Selected = true;
                    this.dgvDetail.CurrentCell.Selected = false;
                    if (!this.Delete_Detail(this.dgvDetail.Rows[this.dgvDetail.CurrentCell.RowIndex]))
                    {
                        return;
                    }
                    this.dgvDetail.Rows.Remove(this.dgvDetail.Rows[this.dgvDetail.CurrentCell.RowIndex]);
                    if (this.dgvDetail.Rows.Count == 0)
                    {
                        this.AddDtlRow(this.dgvDetail.Name);
                    }
                }
            }
        }
        private void AddDtlRow(string strGridName)
        {
            AddDtlRow(strGridName, -1);
        }

        private void AddDtlRow(string strGridName, double iRow)
        {
            if (strGridName == dgvDetail.Name)
            {
                int intRow = CvtFunc.ToInt(iRow);
                m_bCanTrigerCellValueChangedEven = false;
                if (iRow >= 0)
                {
                    intRow++;
                    this.dgvDetail.Rows.Insert(intRow, 1);
                }
                else
                {
                    this.dgvDetail.Rows.Add();
                    intRow = this.dgvDetail.Rows.Count - 1;
                }
                dgvDetail.Rows[intRow].Cells[(int)DtlCol.RowSeqNo].Value = dgvDetail.Rows.Count.ToString();
                dgvDetail.Rows[intRow].Cells[(int)DtlCol.ParentMaterialName].Value = mstrParentMaterialName ;
                dgvDetail.Rows[intRow].Cells[(int)DtlCol.ParentMaterialID].Value =   mstrParentMaterialID  ;
                dgvDetail.Rows[intRow].Cells[(int)DtlCol.CateID].Value = "";
                dgvDetail.Rows[intRow].Cells[(int)DtlCol.CateName].Value = ""; 
                dgvDetail.Rows[intRow].Cells[(int)DtlCol.StandardCost].Value = "0.00";
                dgvDetail.Rows[intRow].Cells[(int)DtlCol.PlanStandardCost].Value = "0.00";
                dgvDetail.Rows[intRow].Cells[(int)DtlCol.StandardCostUnit].Value = "";
                dgvDetail.Rows[intRow].Cells[(int)DtlCol.MaterialID ].Value = "";
                dgvDetail.Rows[intRow].Cells[(int)DtlCol.MaterialName].Value = "";
                dgvDetail.Rows[intRow].Cells[(int)DtlCol.RecID].Value = -1;
                dgvDetail.Rows[intRow].Cells[(int)DtlCol.ValueChanged].Value = "0";
                dgvDetail.CurrentCell = dgvDetail.Rows[intRow].Cells[(int)DtlCol.CateID];
                m_bCanTrigerCellValueChangedEven = true;
            }
        }
        private void Dtl_LookUp(int iColumnIndex)
        {
            string strSQL = "", strCaption = "", strFindValue = "", strFindField = "";
            switch ((DtlCol)iColumnIndex)
            {
                case DtlCol.CateID:
                    strSQL = WP.Commons.BaseFunctions.GetLookUpSQL("rmMaterialType");
                    strCaption = "类别选择";
                    if (this.dgvDetail.CurrentRow.Cells[(int)DtlCol.CateID].Value != null) strFindValue = this.dgvDetail.CurrentRow.Cells[(int)DtlCol.CateID].Value.ToString();
                    strFindField = "";
                    break;
            }

            if (strSQL == "") return;
            if (mblnLookUpSortByDescription)
            {
                strSQL = DataFunctions.GetSqlByDescriptionSort(strSQL);
                mblnLookUpSortByDescription = false;
            }

            frmLookUp frmlookup = new frmLookUp(strSQL, strCaption, strFindValue, strFindField);
            frmlookup.ShowDialog();

            if (frmlookup.HaveReturnValue)
            {
                switch ((DtlCol)iColumnIndex)
                {
                    case DtlCol.CateID:
                        this.dgvDetail.CurrentRow.Cells[(int)DtlCol.CateID].Value = frmlookup.GetReturnValue("MaterialTypeID");
                        this.dgvDetail.CurrentRow.Cells[(int)DtlCol.CateName].Value = frmlookup.GetReturnValue("MaterialType");
                        
                        break;
                }
            }
            frmlookup.Close();
        }
        private bool CheckDtlRow(ref string strErrMsg)
        {
            if (this.dgvDetail.Rows.Count <=0 ) return true;
            if (this.dgvDetail.CurrentCell == null) return true;
            if (this.dgvDetail.CurrentRow == null) return true;
            bool bCheckResult = true;
            for (int i = 0; i < dgvDetail.ColumnCount - 1; i++)
            {

                bCheckResult = CheckDtlCell(i, ref strErrMsg);
                if (!bCheckResult) break;
            }
            string strRecID = CvtFunc.ToString(this.dgvDetail.CurrentRow.Cells[(int)DtlCol.RecID].Value);
            string strMaterialID = CvtFunc.ToString(this.dgvDetail.CurrentRow.Cells[(int)DtlCol.MaterialID].Value);
            string strMaterialName = CvtFunc.ToString(this.dgvDetail.CurrentRow.Cells[(int)DtlCol.MaterialName].Value);
            string strSQL = " select * from " + mstrTableName + " where RecID!='" + strRecID + "' and ParentMaterialID='"+ mstrParentMaterialID +"'";
            strSQL += " and (MaterialTypeID='" + strMaterialID + "' )";
            DataRow drCheck = mdbcon.GetDataFirstRow(strSQL);
            if (drCheck != null)
            {
                strErrMsg = "此设置已经存在，不能重复添加。";
                return false;
            }
            return bCheckResult;
        }
        private bool CheckDtlCell(int iColumn, ref string strErrMsg)
        {
            bool bReturnValue = true;
            strErrMsg = "";
            string strValue = "";
            if (this.dgvDetail.CurrentRow != null && this.dgvDetail.CurrentRow.Cells[iColumn].Value != null)
            {
                strValue = this.dgvDetail.CurrentRow.Cells[iColumn].Value.ToString().TrimEnd();
            }
            switch ((DtlCol)iColumn)
            {
                case DtlCol.MaterialID:
                    if (strValue.Trim() == "")
                    {
                        strErrMsg = "类别不能空.";
                        return false;
                    }
                    return true;
            }
            return bReturnValue;
        }
        private void dgvDetail_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            if (dgvDetail.CurrentRow == null)
            {
                e.Cancel = false;
                return;
            }
            if (int.Parse(dgvDetail.CurrentRow.Cells[(int)DtlCol.RecID].Value.ToString()) > 0 && int.Parse(dgvDetail.CurrentRow.Cells[(int)DtlCol.ValueChanged].Value.ToString()) == 0)
            {
                e.Cancel = false;
                return;

            }

            if (mhtiDtl != null && mhtiDtl.ColumnIndex == (int)DtlCol.DelButton)
            {
                mhtiDtl = null;
                return;
            }


            if (this.dgvDetail.CurrentCell.ColumnIndex == (int)DtlCol.DelButton)
            {
                e.Cancel = false;
                return;

            }
            string strErrMsg = "";
            if (!BaseFunctions.NeedValidGridCell(this.ActiveControl, "dgvDetail") || m_bSettingStatus || !m_bCanTrigerCellValueChangedEven)
            {
                e.Cancel = false;
                return;
            }

            e.Cancel = !CheckDtlCell(e.ColumnIndex, ref strErrMsg);
            if (strErrMsg != "")
            {
                MessageBox.Show(strErrMsg, SysVar.WarnTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }
        private void dgvDetail_RowValidating(object sender, DataGridViewCellCancelEventArgs e)
        {
            if (dgvDetail.CurrentRow != null && int.Parse(dgvDetail.CurrentRow.Cells[(int)DtlCol.RecID].Value.ToString()) > 0 && int.Parse(dgvDetail.CurrentRow.Cells[(int)DtlCol.ValueChanged].Value.ToString()) == 0)
            {
                e.Cancel = false;
                return;

            }

            string strErrMsg = "";
            if (!BaseFunctions.NeedValidGridCell(this.ActiveControl, "dgvDetail") || m_bSettingStatus || !m_bCanTrigerCellValueChangedEven)
            {
                e.Cancel = false;
                return;
            }
            e.Cancel = !CheckDtlRow(ref strErrMsg);
            if (strErrMsg != "")
            {
                MessageBox.Show(strErrMsg, SysVar.WarnTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }

        }
        private void dgvDetail_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (!m_bCanTrigerCellValueChangedEven || e.ColumnIndex == (int)DtlCol.ValueChanged || e.ColumnIndex ==(int)DtlCol.DelButton) return;
            dgvDetail.Rows[e.RowIndex].Cells[(int)DtlCol.ValueChanged].Value = "1";
            string strValue = CvtFunc.ToString ( this.dgvDetail.CurrentRow.Cells[(int)DtlCol.CateID].Value);
            if (e.ColumnIndex == (int)DtlCol.CateID)
            {   
                 
                this.dgvDetail.CurrentRow.Cells[(int)DtlCol.CateName].Value = DataFunctions.GetFieldValue ("rmMaterialType","MaterialType","MaterialTypeID='"+ strValue +"'");
            }        

            m_bDtlChanged = true;
            dgvDetail.EndEdit();          
        }
        private void dgvDetail_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            if (this.dgvDetail.CurrentCell.ColumnIndex == (int)DtlCol.DelButton)
            {
                this.dgvDetail.CurrentCell.Selected = false;
            }

            if (this.dgvDetail.CurrentRow == null) return;
            if (CvtFunc.ToDouble(this.dgvDetail.CurrentRow.Cells[(int)DtlCol.RecID].Value) > 0)
            {
                this.dgvDetail.CurrentRow.Cells[(int)DtlCol.MaterialID].ReadOnly = true;
                //this.dgvDetail.CurrentRow.Cells[(int)DtlCol.MaterialName].ReadOnly = true;
            }
        }
        private void dgvDetail_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (this.dgvDetail.CurrentCell.ColumnIndex == (int)DtlCol.DelButton)
            {
                this.dgvDetail.CurrentCell.Selected = false;
            }
        }
        private void dgvDetail_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            return;
        }
        private void dgvDetail_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            base.SetGridViewDelImage(this.dgvDetail, e, (int)DtlCol.DelButton);
            string strErrMsg = "";
            if (dgvDetail.CurrentRow != null && CvtFunc.ToInt(dgvDetail.CurrentRow.Cells[(int)DtlCol.ValueChanged].Value) > 0)
            {
                if (CheckDtlRow(ref strErrMsg))
                {
                    if (!Update_Detail(this.dgvDetail.Rows[this.dgvDetail.CurrentCell.RowIndex]))
                    {
                        return;
                    }

                    //AddDtlRow(this.dgvDetail.Name);
                }
                else
                {
                    return;
                }
            }
        }
        private void dgvDetail_MouseDown(object sender, MouseEventArgs e)
        {

            mhtiDtl = this.dgvDetail.HitTest(e.X, e.Y);

            //System.Windows.Forms.DataGridView.HitTestInfo hti;
            //hti = this.dgvDetail.HitTest(e.X, e.Y);
            //switch (hti.Type)
            //{
            //    case System.Windows.Forms.DataGridViewHitTestType.Cell:
            //        break;
            //}
        }
        private bool dgvDetail_KeyDownEvent(System.Windows.Forms.Keys keyData)
        {
            string strErrMsg = "";
            int j;
            switch (keyData)
            {
                case Keys.Down:
                    #region Key Down

                    this.dgvDetail.EndEdit();
                    if (CvtFunc.ToInt(dgvDetail.CurrentRow.Cells[(int)DtlCol.RecID].Value) > 0)
                    {
                        if (CvtFunc.ToInt(dgvDetail.CurrentRow.Cells[(int)DtlCol.ValueChanged].Value) > 0)
                        {

                            if (CheckDtlRow(ref strErrMsg))
                            {

                                if (!Update_Detail(this.dgvDetail.Rows[this.dgvDetail.CurrentCell.RowIndex]))
                                {
                                    return true;
                                }
                            }
                            else
                            {
                                MessageBox.Show(strErrMsg, SysVar.WarnTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                return true;
                            }
                        }
                        if (dgvDetail.CurrentCell.RowIndex < dgvDetail.Rows.Count - 1)
                        {
                            dgvDetail.CurrentCell = this.dgvDetail.Rows[dgvDetail.CurrentCell.RowIndex + 1].Cells[(int)DtlCol.CateID];
                        }
                        else
                        {
                            AddDtlRow(this.dgvDetail.Name);
                        }
                    }
                    else
                    {
                        if (CheckDtlRow(ref strErrMsg))
                        {
                            if (!Update_Detail(this.dgvDetail.Rows[this.dgvDetail.CurrentCell.RowIndex]))
                            {
                                return true;
                            }
                            AddDtlRow(this.dgvDetail.Name);
                        }
                        else
                        {
                            MessageBox.Show(strErrMsg, SysVar.WarnTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            return true;
                        }
                    }
                    return true;


                    #endregion
                case Keys.Up:
                    #region Key Up
                    if (CvtFunc.ToInt(dgvDetail.CurrentRow.Cells[(int)DtlCol.RecID].Value) > 0)
                    {
                        if (CvtFunc.ToInt(dgvDetail.CurrentRow.Cells[(int)DtlCol.ValueChanged].Value) > 0)
                        {
                            if (CheckDtlRow(ref strErrMsg))
                            {
                                if (!Update_Detail(this.dgvDetail.Rows[this.dgvDetail.CurrentCell.RowIndex])) return true;
                            }
                            else
                            {
                                MessageBox.Show(strErrMsg, SysVar.WarnTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                return true;
                            }
                        }
                        if (dgvDetail.CurrentCell.RowIndex > 0)
                        {
                            dgvDetail.CurrentCell = this.dgvDetail.Rows[dgvDetail.CurrentCell.RowIndex - 1].Cells[(int)DtlCol.CateID ];
                        }
                    }
                    else
                    {
                        if (CheckDtlRow(ref strErrMsg))
                        {
                            if (!Update_Detail(this.dgvDetail.Rows[this.dgvDetail.CurrentCell.RowIndex])) return true;
                            if (dgvDetail.CurrentCell.RowIndex > 0)
                            {
                                dgvDetail.CurrentCell = this.dgvDetail.Rows[dgvDetail.CurrentCell.RowIndex - 1].Cells[(int)DtlCol.MaterialID];
                            }
                        }
                        else if (dgvDetail.Rows.Count > 1)
                        {

                            this.dgvDetail.Rows.Remove(this.dgvDetail.CurrentRow);
                            dgvDetail.CurrentCell = this.dgvDetail.Rows[dgvDetail.Rows.Count - 1].Cells[(int)DtlCol.MaterialID];
                        }
                    }
                    return true;
                    #endregion
                case Keys.Enter:
                    #region Key Enter
                    this.dgvDetail.EndEdit();
                    j = BaseFunctions.GetGridViewLastEnabledEditColumn(this.dgvDetail);
                    if (dgvDetail.CurrentCell.ColumnIndex == j)
                    {
                        if (CvtFunc.ToInt(dgvDetail.CurrentRow.Cells[(int)DtlCol.RecID].Value) > 0)
                        {
                            if (CvtFunc.ToInt(dgvDetail.CurrentRow.Cells[(int)DtlCol.ValueChanged].Value) > 0)
                            {
                                if (CheckDtlRow(ref strErrMsg))
                                {
                                    if (!Update_Detail(this.dgvDetail.Rows[this.dgvDetail.CurrentCell.RowIndex])) return true;
                                }
                                else
                                {
                                    MessageBox.Show(strErrMsg, SysVar.WarnTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                    return true;
                                }
                            }
                        }
                        else
                        {
                            if (CheckDtlRow(ref strErrMsg))
                            {
                                if (!Update_Detail(this.dgvDetail.Rows[this.dgvDetail.CurrentCell.RowIndex])) return true;
                            }
                            else
                            {
                                MessageBox.Show(strErrMsg, SysVar.WarnTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                return true;
                            }

                        }
                        if (dgvDetail.CurrentCell.RowIndex < dgvDetail.Rows.Count - 1)
                        {
                            dgvDetail.CurrentCell = this.dgvDetail.Rows[dgvDetail.CurrentCell.RowIndex + 1].Cells[(int)DtlCol.CateID];
                        }
                        else
                        {
                            AddDtlRow(this.dgvDetail.Name);
                        }
                    }
                    else
                    {
                        j = BaseFunctions.GetGridViewNextEnabledEditColumn(dgvDetail, dgvDetail.CurrentCell.ColumnIndex);
                        if (j >= 0) SendKeys.Send("{Tab}");
                        //  if (j >= 0) dgvDetail.CurrentCell = this.dgvDetail.Rows[dgvDetail.CurrentCell.RowIndex].Cells[j];
                    }

                    return true;
                    #endregion
            }
            return false;
        }
        private bool Update_Detail(DataGridViewRow dgvRow)
        {
            if (dgvRow == null) return false;
            string strSQL = string.Empty;
            this.dgvDetail.EndEdit();
            DBAccess dbcon = new DBAccess(false, false);
            string strFields = "ParentMaterialID,CateID,CateName,MaterialTypeID,MaterialTypeName,StandardCost,PlanStandardCost,StandardCostUnit";
            try
            {
                DataRow drDtl = dbcon.GetDataTable("select * from " + mstrTableName + " ").NewRow();
                if (CvtFunc.ToDouble(dgvRow.Cells[(int)DtlCol.RecID].Value) > 0)   //Update
                {
                    BaseFunctions.FillDataRowByGridRow(dgvRow, drDtl);
                    DataFunctions.UpdateTable(dbcon, strFields, mstrTableName, drDtl, FormStatus.Update);
                }
                else //Add New
                {
                    BaseFunctions.FillDataRowByGridRow(dgvRow, drDtl);
                    DataFunctions.UpdateTable(dbcon, strFields, mstrTableName, drDtl, FormStatus.AddNew);
                    dgvRow.Cells[(int)DtlCol.RecID].Value = CvtFunc.ToString(drDtl["RecID"]);
                }
                dbcon.CloseCon();
            }
            catch (Exception ex)
            {
                dbcon.CloseCon();
                return false;
            }
            return true;
        }
        private bool Delete_Detail(DataGridViewRow dgvRow)
        {
            if (dgvRow == null) return true;
            string strRecID = CvtFunc.ToString(dgvRow.Cells[(int)DtlCol.RecID].Value);
            string strSeqNo = CvtFunc.ToString(dgvRow.Cells[(int)DtlCol.RowSeqNo].Value);
            string strSecondMaterialTypeID = CvtFunc.ToString ( dgvRow.Cells[(int)DtlCol.MaterialID].Value);   
            string strSQL = string.Empty;
            if (CvtFunc.ToDouble(strRecID) > 0)
            {
                if (DataFunctions.CheckRecordExist("iminvmaster", strSecondMaterialTypeID, "SecondMaterialTypeID"))
                {
                    MsgBoxEx.Show("些价格类别已经使用，不能删除。", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return false ;
                }

                if (MessageBox.Show("您确定要删除？", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    DBAccess dbcon = new DBAccess(false, false);
                    try
                    {
                        dbcon.BeginTran();
                        strSQL = "Delete " + mstrTableName + " where RecID='" + strRecID + "'";
                        dbcon.ExecuteSql(strSQL);
                        dbcon.CommitTran();
                    }
                    catch (Exception ex)
                    {
                        dbcon.RollbackTran();
                    }
                    dbcon.CloseCon();
                    return true;
                }
            }
            return false ;
           
        }

        private void LookUp_Click(object sender, EventArgs e)
        {
            string strFindValue = string.Empty;
            string strFieldName = string.Empty;
            string strSql = string.Empty;
            string strCaption = string.Empty;
            Control con = sender as Control;
            if (con == null) return;
            Control ocon = null;
            switch (con.Name)
            {
                case "ubtCategoryName":
                    strFindValue = txtCategoryName.Text;
                    strFieldName = "CategoryName";
                    strSql = BaseFunctions.GetLookUpSQL("rmCategory");
                    ocon = txtCategoryName;
                    break;
            }
            if (mblnLookUpSortByDescription) strSql = DataFunctions.GetSqlByDescriptionSort(strSql);
            mblnLookUpSortByDescription = false;

            frmLookUp frmlookup = new frmLookUp(strSql, strCaption, strFindValue, strFieldName);
            frmlookup.ShowDialog();
            if (frmlookup.HaveReturnValue)
            {
                ocon.Text = frmlookup.GetReturnValue(strFieldName);
                ocon.Focus();
            }
            frmlookup.Close();

        }
        #endregion

        private void txtCategoryName_TextChanged(object sender, EventArgs e)
        {
            FillDtl(txtCategoryName.Text);
        }

        private void txtCategoryName_Validating(object sender, CancelEventArgs e)
        {
            if (txtCategoryName.Text == "" && this.dgvDetail.Rows.Count == 0) return;
            //if (!mPressExit)
            //{
            //    if (txtCategoryName.Text == "") e.Cancel = true;
            //}
        }

        private void dgvDetail_CellLeave(object sender, DataGridViewCellEventArgs e)
        {
            this.dgvDetail.EndEdit();
            if (e.ColumnIndex == (int)DtlCol.StandardCost )
            {
                this.dgvDetail.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = BaseFunctions.FormatNumeric(this.dgvDetail.Rows[e.RowIndex].Cells[e.ColumnIndex].Value, "Ns.4");
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (tbcMain.SelectedTab == tbpGeneral)
            {
                if (!this.UpdateData()) return;               
            }
            else if ( tbcMain.SelectedTab == tabPage1 )
            {
                string strErrMsg = string.Empty;
                if (CheckDtlRow(ref strErrMsg))
                {
                    foreach (DataGridViewRow dgvRow in this.dgvDetail.Rows)
                    {
                        if (!this.Update_Detail(dgvRow))
                        {
                            break;
                        }
                    }
                    //MsgBoxEx.Show("成功保存！", SysVar.InformationTitle);
                }
            }
            base.SetStatusAfterUpdate();
        }

        private void btnToExcel_Click(object sender, EventArgs e)
        {

            string strSQL = "select MaterialTypeID,MaterialTypeName,StandardCost,StandardCostUnit,RecID from rmSecondMaterialType where ParentMaterialID='" + mstrParentMaterialID + "'";
            DataView dv = mdbcon.GetDataTable(strSQL).DefaultView;
            ToExcel.DataGirdViewToExcel(this.dgvDetail);
        }

        private void RefreshSeqNo()
        {
            foreach (DataGridViewRow dgvRow in this.dgvDetail.Rows)
            {
                dgvRow.Cells[(int)DtlCol.RowSeqNo].Value = CvtFunc.ToString(dgvRow.Index + 1); 
            }
            this.dgvDetail.EndEdit();
        }

        private void dgvDetail_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e)
        {
            RefreshSeqNo();
        }

        private void ubtCategoryName_Click(object sender, EventArgs e)
        {
            Control con = sender as Control;
            if (con == null) return;
            if (con == this.ubtCategoryName )
            {
                string strSql = string.Empty;
                {
                    strSql = "select MaterialTypeID as 价格类别代码	,MaterialTypeName as 价格类别名称,StandardCost as 常用单价	,StandardCostUnit as 单位 from rmSecondMaterialType";
                }

                string strFindValue = txtCategoryName.Text;
                string strLookUpField = "MaterialTypeName";
                frmLookUp frmlookup = new frmLookUp(strSql, "", strFindValue, strLookUpField); ////, bNewEditEnabled);

                frmlookup.ShowDialog();
                if (frmlookup.HaveReturnValue)
                {
                    txtCategoryName.Text = frmlookup.GetReturnValue("MaterialTypeName");

                }
                frmlookup.Close();

            }
        }

        private void frmRMSecondCategoryMtn_FormClosing(object sender, FormClosingEventArgs e)
        {
            DataFunctions.SetGridColumnWidthString(this.Name, this.CurrentMenuInfo.iParameter, SysVar.UserName, this.dgList);
        }


        #region ***Form Functions

        public override bool UpdateData()
        {
            bool bUpdateResult = true;
            if (base.ConfirmChange())
            {
                bUpdateResult = base.UpdateData();
            }
            return bUpdateResult;
        }
        public override bool ConfirmChange()
        {
            return (base.ConfirmChange());
        }
        
        protected override bool FieldCheck(string strFieldValue, string strFieldName, ref string strErrPrompt)
        {
            strErrPrompt = string.Empty;
            DBAccess dBAccess = new DBAccess(false, false);
            DataRow dr = base.GetCurrentMtnDataRow();
            
            RM.BusinessRule.RMSecondMaterialTypeBRL mBRL = new WP.RM.BusinessRule.RMSecondMaterialTypeBRL();
            bool bCheckResult = mBRL.FieldCheck(dBAccess, dr, strFieldName, ref strErrPrompt, this.formStatus);
            dBAccess.CloseCon();
            return bCheckResult;
        }

        protected override string GetLookUpSQL(string strTableName, string strLookUpControlName)
        {
            string strSql = string.Empty;
            return base.GetLookUpSQL(strTableName, strLookUpControlName);
        }
        public override void SetButtonStatus()
        {
            switch (this.formStatus)
            {
                case FormStatus.AddNew:
                    BaseFunctions.ControlEnabled(this.toolStrip, false);
                    this.btnUpdate.Enabled = true;
                    this.btnDelete.Enabled = false;
                    this.btnNew.Enabled = true;
                    this.btnCancel.Enabled = true;
                    this.btnUpdatePrice.Enabled = false;
                    break;
                case FormStatus.Delete:
                    BaseFunctions.ControlEnabled(this.toolStrip, false);
                    this.btnUpdate.Enabled = false;
                    this.btnDelete.Enabled = false;
                    this.btnNew.Enabled = (true && this.CurrentMenuInfo.strPermit == "Y");
                    this.btnCancel.Enabled = false;
                    this.btnUpdatePrice.Enabled = this.btnNew.Enabled;
                    break;
                case FormStatus.Query:
                    BaseFunctions.ControlEnabled(this.toolStrip, false);
                    this.btnUpdate.Enabled = false;
                    this.btnDelete.Enabled = false;
                    this.btnNew.Enabled = (true && this.CurrentMenuInfo.strPermit == "Y");
                    this.btnCancel.Enabled = false;
                    this.btnUpdatePrice.Enabled = this.btnNew.Enabled;
                    break;
                case FormStatus.Update:
                    BaseFunctions.ControlEnabled(this.toolStrip, false);
                    this.btnUpdate.Enabled = true;
                    this.btnDelete.Enabled = true;
                    this.btnNew.Enabled = true;
                    this.btnCancel.Enabled = true;
                    this.btnUpdatePrice.Enabled = false;
                    break;
                case FormStatus.Loading:
                    break;

            }

            this.btnToExcel.Visible = (this.formStatus == FormStatus.Delete || this.formStatus == FormStatus.Query) && SysVar.AbleToExcel;
            this.btnToExcel.Enabled = this.formStatus == FormStatus.Delete || this.formStatus == FormStatus.Query;

            newToolStripMenuItem.Enabled = btnNew.Enabled;
            cancelToolStripMenuItem.Enabled = btnCancel.Enabled;
            deleteToolStripMenuItem.Enabled = btnDelete.Enabled;
            saveToolStripMenuItem.Enabled = btnUpdate.Enabled;

            this.btnTop.Enabled = true;
            this.btnBottom.Enabled = true;
            this.btnPrev.Enabled = true;
            this.btnNext.Enabled = true;
            this.btnHelp.Enabled = true;
            this.btnExit.Enabled = true;
        }
        public override void SetOtherStatus()
        {
            mblnSettingStatus = true;

            switch (this.formStatus)
            {
                case FormStatus.AddNew:
                    this.txtMaterialTypeID.Focus();
                    break;
                case FormStatus.Delete:
                    break;
                case FormStatus.Query:
                    break;
                case FormStatus.Update:
                    this.txtMaterialTypeName.Focus();
                    break;
                case FormStatus.Loading:
                    break;
            }

            mblnSettingStatus = false;
        }
        public override void RefreshOverView()
        {
            base.RefreshOverView();
            if (this.RecID != -1)
                this.dgList.RecID = this.RecID;
        }

        private void FillLookUpInfo()
        {
            //SetFieldLookUpInfo("MaterialTypeID", "rmMaterialType", "CateID", "ubtCateID");
            //SetFieldLookUpInfo("MaterialType", "rmMaterialType", "CateName", "ubtCateID");
            //SetFieldLookUpInfo("MaterialFItemID", "rmMaterialType", "CateFItemID", "ubtCateID");

            SetFieldLookUpInfo("CateID","rmMaterialType",  "MaterialTypeID", "ubtCateID");
            SetFieldLookUpInfo("CateName", "rmMaterialType", "MaterialType", "ubtCateID");
            SetFieldLookUpInfo("CateFItemID","rmMaterialType", "MaterialFItemID",  "ubtCateID");
        }

        #endregion Form Functions***

        private void btnUpdateToMarketCost_Click(object sender, EventArgs e)
        {
            txtPlanStandardCost.Text = txtStandardCost.Text; 
        }

        private void tvTree_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (e.Node.Nodes.Count == 0) //最后一层
            {
                string strMaterialID = "";
                strMaterialID = CvtFunc.ToString(e.Node.Text.ToString()).Split('-')[0];
                //this.dgList.FindRecord(strMaterialID, "MaterialTypeID");
                string strCond = " MaterialTypeID like '" + strMaterialID + "%'";
                ShowOverViewGrid(strCond);
            }
            else  //最上层
            {
                string strMaterialID = "";
                strMaterialID = CvtFunc.ToString(e.Node.Text.ToString()).Split('-')[0];
                //this.dgList.FindRecord(strMaterialID, "MaterialTypeID");
                string strCond = " MaterialTypeID like '" + strMaterialID + "%'";
                ShowOverViewGrid(strCond);
            }
        }

        private void ShowOverViewGrid(string strWhereCondition)
        {
            string strSql = "";
            if (dgList.Sql.Trim() == "")
                strSql = WP.Commons.BaseFunctions.GetLookUpSQL(this.TableName, this.CurrentMenuInfo.iParameter);
            else
                strSql = dgList.Sql;

            if (strWhereCondition != "")
            {
                string strOrderBy = BaseFunctions.GetSqlParts(strSql, SqlOption.SqlPartOrderBy);
                strSql = BaseFunctions.GetSqlParts(strSql, SqlOption.SqlPartMain) + " where " + strWhereCondition;
                strSql += strOrderBy;
            }
            //string strWhere = BaseFunctions.GetSqlParts(strSql, SqlOption.SqlPartWhere);
            //mstrWhereCondition = strWhere;
            this.dgList.Sql = strSql;
        }

        private void btnUpdatePrice_Click(object sender, EventArgs e)
        {
            string strSQL="update rmSecondMaterialType set PlanStandardCost = StandardCost";
            new DBAccess().ExecuteSql(strSQL);
            MsgBoxEx.Show("操作完成");  
        }
    }
}
